package com.example.designloginandregister;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText et1, et2;//创建EditText对象
    private  String userName,passwd,spPsw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //注册按钮对象
        Button b = (Button) findViewById(R.id.bt);
        //登录按钮对象
        Button b1 = (Button) findViewById(R.id.bt1);
        //修改密码按钮对象
        Button entermodify_bt = (Button) findViewById(R.id.EnterModify_bt);
        //忘记密码按钮对象
        Button forget_bt = (Button) findViewById(R.id.forget_bt);
        //账号输入框对象
        et1 =(EditText) findViewById(R.id.et1);
        //密码输入框对象
        et2 =(EditText) findViewById(R.id.et2);
        //注册点击事件
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Register.class);
                //                intent.putExtra("account","firstuser");
//                intent.putExtra("password","123456");
                startActivity(intent);
            }
        });
        //登录的点击事件
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //获取账号密码框的内容
                userName = et1.getText().toString().trim();
                passwd = et2.getText().toString().trim();
                //对密码进行加密
                String md5Psw= MD5Util.md5(passwd);
                //读取密码
                spPsw=readPsw(userName);
                if (TextUtils.isEmpty(userName)) {//判断账号是否为空
                    Toast.makeText(MainActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(passwd)) {//判断密码是否为空
                    Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else if (md5Psw.equals(spPsw)) {//判断加密的密码是否正确
                    //提示登录成功
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    //存入登录状态
                    editor.putBoolean("islogin", true);
                    //存入账号
                    editor.putString("loginUsername", userName);
                    //提交修改
                    editor.commit();
                    Intent intent = new Intent(MainActivity.this, Login.class);
                    intent.putExtra("islogin", true);
                    setResult(RESULT_OK, intent);
                    finish();
                    startActivity(intent);
                    return;
                } else if ((spPsw!=null&&!TextUtils.isEmpty(spPsw)&&!md5Psw.equals(spPsw)))
                {//判断密码是否正确
                    Toast.makeText(MainActivity.this, "密码不一致 ", Toast.LENGTH_SHORT).show();
                } else {//判断有无此账号
                    Toast.makeText(MainActivity.this, "此用户不存在", Toast.LENGTH_SHORT).show();
                }
            }

        });
        //修改密码点击事件
        entermodify_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,Modify.class);
                startActivity(intent);
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            String username = data.getStringExtra("userName");
            if (!TextUtils.isEmpty(username));
            et1.setText(username);
            et1.setSelection(username.length());
        }
    }
    private String readPsw(String userName){
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        return  sp.getString(userName,"");
    }
}
